/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package it.davial.mgwt.client.activities.picture;

import it.davial.mgwt.client.Area;
import it.davial.mgwt.client.activities.BaseViewImpl;
import it.davial.mgwt.client.resources.ResourceAware;
import it.davial.mgwt.client.ui.widget.AreaWidget;
import it.davial.mgwt.client.ui.widget.MapWidget;

import java.util.List;
import java.util.logging.Logger;

import org.soul.mgwt.client.widget.CsImage;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.dom.client.event.tap.HasTapHandlers;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.MGWTStyle;
import com.googlecode.mgwt.ui.client.widget.HeaderButton;
import com.googlecode.mgwt.ui.client.widget.HeaderPanel;

/**
 * Sample implementation of {@link PictureView}.
 */
public class PictureViewImpl extends BaseViewImpl implements PictureView,
		ResourceAware {
	private static final Logger logger = Logger.getLogger("MainView");

	private Presenter listener;
	private Button button;
	private HTMLPanel panel;

	public PictureViewImpl() {
		panel = new HTMLPanel("");
		if (MGWT.getOsDetection().isPhone()) {
			button = new Button("back");
			button.getElement().setAttribute("style",
					"margin:auto;width: 200px;display:block");
			panel.add(button);
			headerBackButton.removeFromParent();
		}

		scrollPanel.setWidget(panel);
		scrollPanel.setScrollingEnabledX(false);

		final CsImage planets = new CsImage();
		planets.setResource(mainResources.planets());
		panel.add(planets);
		AreaWidget sun = new AreaWidget("rect", "0 0 82 126", "Sun",
				new SolarSystemCommand("Sun"));
		AreaWidget mercury = new AreaWidget("circle", "90 58 3", "Mercury",
				new SolarSystemCommand("Mercury"));
		AreaWidget venus = new AreaWidget("circle", "124 58 8", "Venus",
				new SolarSystemCommand("Venus"));
		// CREATE MAP
		final MapWidget map = new MapWidget(new AreaWidget[] { sun, mercury,
				venus });
		map.setID("planetmap");
		map.setName("planetmap");
		panel.add(map);
		// wait till image is loaded to bind map
		planets.addLoadHandler(new LoadHandler() {

			@Override
			public void onLoad(LoadEvent event) {
				map.bindImage(planets);

			}
		});

		/*final Image man = new Image();
		man.setResource(mainResources.man());
		panel.add(man);

		AreaWidget hand = new AreaWidget("poly",
				"256 346 258 378 316 373 315 309", "hand",
				new SolarSystemCommand("Hand"));

		AreaWidget leg = new AreaWidget("poly",
				"275 402 317 369 312 413 364 431", "Leg",
				new SolarSystemCommand("Leg"));

		// CREATE MAP
		final MapWidget mapMan = new MapWidget(new AreaWidget[] { hand, leg });
		mapMan.setID("mapMan");
		mapMan.setName("mapMan");
		panel.add(mapMan);
		// wait till image is loaded to bind map
		man.addLoadHandler(new LoadHandler() {

			@Override
			public void onLoad(LoadEvent event) {
				mapMan.bindImage(man);

			}
		});*/

	}

	@Override
	public void setPresenter(Presenter listener) {
		this.listener = listener;
	}

	class SolarSystemCommand implements Command {

		String idFigure;

		public SolarSystemCommand(String idFigure) {
			this.idFigure = idFigure;
		}

		public void execute() {
			callFillBodyArea(idFigure);
		}
	}
    
	private void callFillBodyArea(String idFigure){
		PictureActivity pictureActivity=(PictureActivity)listener;
		pictureActivity.fillBodyArea(Integer.parseInt(idFigure),(PictureView)this);
		Window.alert("You've clicked: " + idFigure);
	}
	
	@Override
	public void setBodyArea(int idFigure,List<Area> list) {
		panel.clear();
		final Image man = new Image();
		switch (idFigure) {
			case 1:
				man.setResource(mainResources.man());
				break;
			case 2:
				man.setResource(mainResources.planets());
				break;

		}
		
		panel.add(man);
		
		int i=0;
		AreaWidget[] areaWidget=new AreaWidget[list.size()];
		for (Area area:list){
			        String idFigureChild=String.valueOf(area.getIdFigureChild());
					AreaWidget part = new AreaWidget("poly",
					area.getPolygon(), idFigureChild,
					new SolarSystemCommand(idFigureChild));
					areaWidget[i]=part;
					i++;
		 }
		final MapWidget mapMan = new MapWidget(areaWidget);
		mapMan.setID("mapMan");
		mapMan.setName("mapMan");
		panel.add(mapMan);
		man.addLoadHandler(new LoadHandler() {

			@Override
			public void onLoad(LoadEvent event) {
				mapMan.bindImage(man);

			}
		});
	}

}
